package com.mall.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.mall.common.constant.OrderConstant;
import com.mall.common.dto.OrderDTO;
import com.mall.common.dto.OrderItemDTO;
import com.mall.payment.config.AlipayConfig;
import com.mall.payment.config.PayTemplate;
import com.mall.payment.entity.PaymentEntity;
import com.mall.payment.feign.OrderFeignClient;
import com.mall.payment.service.AlipayService;
import com.mall.payment.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


/**
 * @author: 谢君臣
 * @Date: 2022/7/2-19:30
 * @version: 1.0
 * @function:
 */
@Slf4j
@Service
public class AlipayServiceImpl implements AlipayService {
    @Resource
    private PaymentService paymentService;
    @Resource
    private OrderFeignClient orderFeignClient;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private PayTemplate payTemplate;

    @Transactional
    @Override
    public String payOrder(String orderId) throws RuntimeException, AlipayApiException {
        OrderDTO orderDTO = null;
        String order_json_string = redisTemplate.opsForValue().get(OrderConstant.ORDER_PAY_PREFIX + orderId);
        if (StringUtils.isEmpty(order_json_string)){
            orderDTO = orderFeignClient.getByOrderSn(orderId).getData();
        }else {
            orderDTO = JSON.parseObject(order_json_string,OrderDTO.class);
        }
        PaymentEntity paymentEntity = new PaymentEntity();
        paymentEntity.setOrderId(orderId);
        if (orderDTO.getItems()!=null){
            List<String> collect = orderDTO.getItems().stream().map(OrderItemDTO::getSkuName).collect(Collectors.toList());
            paymentEntity.setContent(collect.toString());}
        else paymentEntity.setContent("未知商品");
        paymentEntity.setPaymentType("alipay");
        paymentEntity.setStatus("create");
        paymentEntity.setTransactionId(IdWorker.get32UUID());
        paymentEntity.setPayerTotal(orderDTO.getPayAmount());
        paymentEntity.setTradeType("online");
        paymentEntity.setIsDelete("0");
        paymentEntity.setCreateTime(new Date());
        paymentService.insert(paymentEntity);
        return payTemplate.AliPay(paymentEntity,orderDTO);
    }
}
